from OpenGL.GL import *
from OpenGL.GLUT import *


import math
import numpy as np

parser = None
# points = np.array()


def drwaCricle(r):
    glBegin(GL_LINE_LOOP)
    glColor3f(1, 1, 1)
    for i in range(100):
        glVertex2f(r * math.cos(2 * math.pi / 100 * i),
                   r * math.sin(2 * math.pi / 100 * i))
    glEnd()


def drawPointCloud():
    global parser
    glPointSize(3)
    glBegin(GL_POINTS)
    data = parser.getCircle()
    for i in range(int(len(data)/3)):
        glColor3f(data[3*i+2]/255, 0, 0)
        glVertex2f(data[3*i], data[3*i+1])
        #print("x:%f,y:%f" % (data[3*i], data[3*i+1]))
    glEnd()


def idleFunc():
    pass


def drawFunc():
    # 清除之前画面
    glClear(GL_COLOR_BUFFER_BIT)
    #glRotatef(0.1, 0, 5, 0)
    drwaCricle(0.2)
    drwaCricle(0.4)
    drwaCricle(0.6)
    drawPointCloud()
    # 刷新显示
    glFlush()
    # 使用glut初始化OpenGL


def file_slice(filePath, outPath):
    with open(filePath, 'rb') as input:
        with open(outPath, 'wb') as output:
            input.seek(0)
            data = input.read(1024*1024)
            output.write(data)


if __name__ == "__main__":
    parser = P62Parser('.test/1.P62')
    parser.start()
    glutInit()
    # 显示模式:GLUT_SINGLE无缓冲直接显示|GLUT_RGBA采用RGB(A非alpha)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
    # 窗口位置及大小-生成
    glutInitWindowPosition(100, 100)
    glutInitWindowSize(400, 400)
    glutCreateWindow(b"first")
    # 调用函数绘制图像
    glutDisplayFunc(drawFunc)
    glutIdleFunc(drawFunc)
    # 主循环
    glutMainLoop()
